package com.qdlc.p2p.biz.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qdlc.p2p.biz.executer.AbstractExecuter;
import com.qdlc.p2p.biz.executer.ExecuterHelper;
import com.qdlc.p2p.biz.executer.impl.SetTppDifferenceExecuter;
import com.qdlc.p2p.biz.service.TppDifferenceService;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.util.BigDecimalUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.TppDifferenceDao;
import com.qdlc.p2p.dal.dao.UserDao;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.dto.TppDifference;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.TppDifferenceModel;
import com.qdlc.p2p.dal.util.SystemConfigs;

/**
 * 用户托管资金差额表业务处理
 * @author ygw
 * @version 2.0
 * @date 2015年11月18日 下午4:26:08
 */
@Service("tppDifferenceService")
@Transactional
public class TppDifferenceServiceImpl implements TppDifferenceService {
	
	@Resource
	private TppDifferenceDao tppDifferenceDao;
	
	@Resource
	private UserDao userDao;
	
	@Override
	public PageDataList<TppDifferenceModel> list(TppDifferenceModel model) {
		return tppDifferenceDao.list(model);
	}
	
	@Override
	public void doSetTppDifference(TppDifferenceModel model, Operator operator) {
		
		long userId = model.getUserId();
		double updateUse = BigDecimalUtil.decimal(model.getUpdateUse(),2);
		double updateNoUseMoney = 0.00;// 排除冻结金额（新浪投标托管无冻结，实质为转入中间账户）
		
		// 更新account表(添加资金记录)
		User user = userDao.find(model.getUserId());
		Global.setTransfer("money", updateUse);
		AbstractExecuter setTppDifferenceExecuter = ExecuterHelper.getExecuter(SetTppDifferenceExecuter.class);
		setTppDifferenceExecuter.execute(updateUse,updateUse,updateNoUseMoney, user, new User(SystemConfigs.ADMIN_ID));
		
		// 更新用户托管资金差额表
		TppDifference tppDifference = tppDifferenceDao.findObjByProperty("user.userId", userId);
		// 总额
		tppDifference.setTotal(BigDecimalUtil.add(tppDifference.getTotal(), updateUse));
		// 总额差额
		tppDifference.setDifferenceTotal(BigDecimalUtil.add(tppDifference.getDifferenceTotal(), updateUse));
		// 可用余额
		tppDifference.setUseMoney(BigDecimalUtil.add(tppDifference.getUseMoney(), updateUse));
		// 可用余额差额
		tppDifference.setDifferenceUse(0.00);
		// 更新时间
		tppDifference.setUpdateTime(DateUtil.getNow());
		// 备注
		tppDifference.setRemark(model.getRemark()+"");
		// 处理状态 0：未处理 1：已处理
		tppDifference.setStatus(1);
	    // 更新
		tppDifferenceDao.update(tppDifference);
	}
	
}
